package cn.xd.base.domain.entity;


import cn.xd.base.basic.type.*;
import cn.xd.base.domain.bean.command.AddAdminOrgCommand;
import cn.xd.base.domain.bean.command.UpdateAdminOrgCommand;
import cn.xd.core.basic.common.CreatorId;
import cn.xd.core.basic.common.UpdateTime;
import cn.xd.core.basic.common.UpdaterId;
import cn.xd.common.enums.common.CommonStatusEnum;
import cn.xd.core.ddd.entity.Entity;
import lombok.Builder;
import lombok.Data;

/**
 * 菜单
 */
@Data
@Builder
public class AdminOrg implements Entity<AdminOrgId> {
    private AdminOrgId id;
    private AdminOrgId parentId;
    private Path path;
    private AdminOrgAdditionalApproval additionalApproval;

    private AdminUserId  userId;
    private GlobalUserId referrerId;
    private AdminOrgName name;
    private OrderNum orderNum;
    private AdminOrgType type;
    private AdminOrgStatus status;
    private AdminOrgParams params;
    private CreatorId creatorId;
    private cn.xd.core.basic.common.CreateTime createTime;
    private UpdaterId updaterId;
    private UpdateTime updateTime;

    /**
     * 新增机构
     *
     * @param command
     * @param parentPath
     * @param userId
     * @return
     */
    public static AdminOrg add(AddAdminOrgCommand command, Long userId) {
        return AdminOrg.builder().id(AdminOrgId.of(command.getId())).parentId(AdminOrgId.of(command.getParentId()))
                .name(AdminOrgName.of(command.getName())).orderNum(OrderNum.of(command.getOrderNum()))
                .type(AdminOrgType.of(command.getType())).status(AdminOrgStatus.of(command.getStatus()))
                .additionalApproval(AdminOrgAdditionalApproval.of(command.getAdditionalApproval()))
                .userId(AdminUserId.of(command.getUserId()))
                .referrerId(GlobalUserId.of(command.getReferrerId()))
                .params(AdminOrgParams.of(command.getParams()))
                .creatorId(CreatorId.of(userId)).createTime(cn.xd.core.basic.common.CreateTime.now())
                .updaterId(UpdaterId.of(userId)).updateTime(UpdateTime.now())
                .build();
    }

    /**
     * 更新机构
     *
     * @param command
     * @param parentPath
     * @param userId
     */
    public void update(UpdateAdminOrgCommand command, Long userId) {
        this.parentId = AdminOrgId.of(command.getParentId());

        this.name = AdminOrgName.of(command.getName());
        this.additionalApproval = AdminOrgAdditionalApproval.of(command.getAdditionalApproval());
        this.userId = AdminUserId.of(command.getUserId());
        this.referrerId = GlobalUserId.of(command.getReferrerId());
        this.orderNum = OrderNum.of(command.getOrderNum());
        this.type = AdminOrgType.of(command.getType());
        this.status = AdminOrgStatus.of(command.getStatus());
        this.params = AdminOrgParams.of(command.getParams());
        this.updaterId = UpdaterId.of(userId);
        this.updateTime = UpdateTime.now();
    }

    /**
     * 删除机构
     */
    public void delete() {
        this.setStatus(AdminOrgStatus.of(CommonStatusEnum.DELETED.getCode()));
    }
}
